﻿using System;
using System.Reflection;
using System.Web;
using System.Web.ModelBinding;

namespace PMS.Utils
{
    /// <summary>
    /// 实体方法扩展
    /// </summary>
    public class ModelExtend<T>
    {
        /// <summary>
        /// 给公共部分赋值第一次创建时使用
        /// </summary>
        /// <param name="t"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void SetKeyValue(T t, string key, object value)
        {
            PropertyInfo info = typeof(T).GetProperty(key);
            if (info != null) info.SetValue(t, value);
        }

        /// <summary>
        /// 给公共部分赋值 添加使用
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static void SetAddCommAll(T t)
        {
            SetKeyValue(t, "CreateBy", HttpContext.Current.Session["UserId"].ToInt32());
            SetKeyValue(t, "CreateDate", DateTime.Now);
            SetKeyValue(t, "ModifyBy", 0);
            SetKeyValue(t, "ModifyDate", DateTimeExtend.InitDateTime);
            SetKeyValue(t, "DeleteBy", 0);
            SetKeyValue(t, "DeleteDate", DateTimeExtend.InitDateTime);
            SetKeyValue(t, "Enable", 1);
            SetKeyValue(t, "IsDel", 0);

            //return t;
        }

        /// <summary>
        ///  给公共部分赋值 更新使用
        /// </summary>
        /// <param name="t"></param>

        public static void SetModifyCommAll(T t)
        {
            SetKeyValue(t, "ModifyBy", HttpContext.Current.Session["UserId"].ToInt32());
            SetKeyValue(t, "ModifyDate", DateTime.Now);
            //return t;
        }
    }
}